package com.ruoyi.attendance.service.impl;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.ruoyi.attendance.domain.AttendanceInfo;
import com.ruoyi.attendance.mapper.AttendanceInfoMapper;
import com.ruoyi.common.enums.AttendanceTime;
import com.ruoyi.common.enums.SignStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.attendance.mapper.SignRecordMapper;
import com.ruoyi.attendance.domain.SignRecord;
import com.ruoyi.attendance.service.ISignRecordService;

/**
 * 签到Service业务层处理
 * 
 * @author Weiney
 * @date 2025-03-04
 */
@Service
public class SignRecordServiceImpl implements ISignRecordService 
{
    @Autowired
    private SignRecordMapper signRecordMapper;
    @Autowired
    private AttendanceInfoMapper attendanceInfoMapper;

    /**
     * 查询签到
     * 
     * @param signId 签到主键
     * @return 签到
     */
    @Override
    public SignRecord selectSignRecordBySignId(Long signId)
    {
        return signRecordMapper.selectSignRecordBySignId(signId);
    }

    /**
     * 查询签到列表
     * 
     * @param signRecord 签到
     * @return 签到
     */
    @Override
    public List<SignRecord> selectSignRecordList(SignRecord signRecord)
    {
        return signRecordMapper.selectSignRecordList(signRecord);
    }

    /**
     * 新增签到
     * 
     * @param signRecord 签到
     * @return 结果
     */
    @Override
    public int insertSignRecord(SignRecord signRecord)
    {
        // 准时 迟到 补签 旷课 请假 未签
        // 签到逻辑获取签到发布时间+签到时长，和现在进行比较，> 现在：准时，< 现在：迟到

        // 获取考勤信息对象（包括签到发布时间，签到时长）
        AttendanceInfo attendanceInfo = attendanceInfoMapper.selectAttendanceInfoByCourseRecordId(signRecord.getCourseRecordId());
        AttendanceTime attendanceTime = AttendanceTime.getAttendanceTimeByCode(attendanceInfo.getAttendanceTime());
        // 获取签到发布时间
        Date createTime = attendanceInfo.getCreateTime();
        // 获取签到时长
        Integer duration = attendanceTime.getInfo();
        // 计算签到截止时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(createTime);
        calendar.add(Calendar.MINUTE, duration);
        Date deadline = calendar.getTime();
        // 获取当前时间
        Date now = new Date();
        // 判断签到状态
        SignStatus signStatus;
        if (now.before(deadline)) {
            signStatus = SignStatus.ON_TIME;
        } else {
            signStatus = SignStatus.LATE;
        }
        // 将签到状态设置到 SignRecord 对象中
        signRecord.setSignStatus(signStatus.getCode());
        // 插入数据库
        return signRecordMapper.insertSignRecord(signRecord);
    }

    /**
     * 修改签到
     * 
     * @param signRecord 签到
     * @return 结果
     */
    @Override
    public int updateSignRecord(SignRecord signRecord)
    {
        return signRecordMapper.updateSignRecord(signRecord);
    }

    /**
     * 批量删除签到
     * 
     * @param signIds 需要删除的签到主键
     * @return 结果
     */
    @Override
    public int deleteSignRecordBySignIds(Long[] signIds)
    {
        return signRecordMapper.deleteSignRecordBySignIds(signIds);
    }

    /**
     * 删除签到信息
     * 
     * @param signId 签到主键
     * @return 结果
     */
    @Override
    public int deleteSignRecordBySignId(Long signId)
    {
        return signRecordMapper.deleteSignRecordBySignId(signId);
    }

    /**
     * 根据课程记录 ID 和课程 ID 获取未签到的用户记录列表，并设置签到状态后批量插入
     * @param signRecords 未签到的用户记录列表
     */
    @Override
    public void processUnsignedRecords(List<SignRecord> signRecords) {
        signRecordMapper.insertSignRecords(signRecords);
    }

}
